/**
 * @file ContactPage.jsx
 * @description “联系我们”页面组件。
 */
import React, { useState } from 'react';
import toast from 'react-hot-toast';

const ContactPage = () => {
  const [formData, setFormData] = useState({
    name: '',
    email: '',
    message: '',
  });
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [validated, setValidated] = useState(false); // 新增状态

  const handleChange = (e) => {
    const { id, value } = e.target;
    setFormData((prevData) => ({
      ...prevData,
      [id]: value,
    }));
  };

  const handleSubmit = (event) => {
    event.preventDefault();
    const form = event.currentTarget;
    if (form.checkValidity() === false) {
      event.stopPropagation();
      toast.error('请检查并修正表单中的错误！');
    } else {
      setIsSubmitting(true);
      // console.log("提交的表单数据:", formData);
      setTimeout(() => {
        toast.success('感谢您的留言，我们会尽快与您联系！');
        setFormData({ name: '', email: '', message: '' });
        setIsSubmitting(false);
        setValidated(false); // 重置验证状态以便下次输入
      }, 1500);
    }
    setValidated(true);
  };

  return (
    <div className="container my-5 py-5">
      <div className="row">
        <div className="col-12 mb-4">
          <h1 className="display-5 text-center fw-bold">联系我们</h1>
          <hr />
          <p className="lead text-center text-muted">
            我们很乐意听取您的意见！如果您有任何问题、建议或反馈，请随时通过下面的表单与我们联系。
          </p>
        </div>
      </div>
      <div className="row justify-content-center">
        <div className="col-md-8 col-lg-7">
          <div className="card p-4 shadow-sm">
            <form className={`needs-validation ${validated ? 'was-validated' : ''}`} noValidate onSubmit={handleSubmit}>
              <div className="mb-3">
                <label htmlFor="name" className="form-label">您的姓名</label>
                <input
                  type="text"
                  className="form-control"
                  id="name"
                  placeholder="请输入您的姓名"
                  value={formData.name}
                  onChange={handleChange}
                  required
                  disabled={isSubmitting}
                />
                <div className="invalid-feedback">
                  请输入您的姓名。
                </div>
              </div>
              <div className="mb-3">
                <label htmlFor="email" className="form-label">您的邮箱</label>
                <input
                  type="email"
                  className="form-control"
                  id="email"
                  placeholder="name@example.com"
                  value={formData.email}
                  onChange={handleChange}
                  required
                  disabled={isSubmitting}
                />
                <div className="invalid-feedback">
                  请输入一个有效的邮箱地址。
                </div>
              </div>
              <div className="mb-3">
                <label htmlFor="message" className="form-label">您的留言</label>
                <textarea
                  className="form-control"
                  id="message"
                  rows="5"
                  placeholder="请在此处输入您的留言内容..."
                  value={formData.message}
                  onChange={handleChange}
                  required
                  minLength="10" // 示例：添加最小长度
                  disabled={isSubmitting}
                ></textarea>
                <div className="invalid-feedback">
                  请输入至少10个字符的留言。
                </div>
              </div>
              <div className="text-center">
                <button
                  className="btn btn-primary btn-lg"
                  type="submit"
                  disabled={isSubmitting}
                >
                  {isSubmitting ? '发送中...' : '发送留言'}
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  );
};

export default ContactPage;